package com.baizhi.reflect2;

import java.lang.reflect.Constructor;

/**
 * 通过反射获取构造函数
 */
public class ConstructorDemo {
    public static void main(String[] args) throws NoSuchMethodException {
        Class<Person> pClass = Person.class;

        //获取所有的构造函数
        Constructor<?>[] cons = pClass.getConstructors();
        for (Constructor<?> con : cons) {
            System.out.println(con);
        }

        System.out.println("===============================================");
        //获取一个构造函数
        Constructor<Person> con1 = pClass.getConstructor(null);
        System.out.println(con1);

        Constructor<Person> con2 = pClass.getConstructor(String.class);
        System.out.println(con2);

        Constructor<Person> con3 = pClass.getConstructor(String.class, int.class);
        System.out.println(con3);

        Constructor<Person> con4 = pClass.getConstructor(String.class, int.class, String.class);
        System.out.println(con4);

    }
}
